﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Filmy.Kontroler
{
    class ZewnetrznaBaza
    {
        public string tytul { get; set; }
        public string tytulOryginalny { get; set; }
        public string rokProdukcji { get; set; }
        public string gatunki { get; set; }
        public string rezyserowie { get; set; }
        public string ocena { get; set; }
        public string link { get; set; }
        public string opis { get; set; }
        public string obrazek { get; set; }
        public string czasTrwania { get; set; }
        public string krajePochodzenia { get; set; }
        public PictureBoxSizeMode opcjaWyswietlania { get; set; }


        //pobranie zrodla ze strony
        protected CookieCollection GetCookies(string link, HttpWebRequest request)
        {
            request = (HttpWebRequest)WebRequest.Create(link);
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            request.KeepAlive = true;
            request.Method = "POST";
            request.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            responseStream.Close();
            request.CookieContainer.Add(response.Cookies);
            return response.Cookies;
        }

        protected string dajZrodlo(string link)
        {
            var request = (HttpWebRequest)WebRequest.Create(link);
            // CookieCollection cookies = this.GetCookies(link, request);
            request.Headers.Add(HttpRequestHeader.Cookie, "welcomeScreenNew=welcomeScreen");
            request.Headers.Add(HttpRequestHeader.Cookie, "jetbackup=0");
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            request.KeepAlive = true;
            //request.CookieContainer = new CookieContainer();
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            /*if (cookies != null)
            {
                request.CookieContainer.Add(cookies);
            }*/
            Stream requestStream = request.GetRequestStream();
            var response = request.GetResponse();
            var stream = response.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            var reader = new StreamReader(stream, Encoding.UTF8);
            string zrodlo = reader.ReadToEnd();
            reader.Close();
            response.Close();
            requestStream.Close();
            stream.Close();
            request.Abort();
            zrodlo = zrodlo.Replace("\n", "");
            zrodlo = zrodlo.Replace("\t", "");
            return zrodlo;
        }

        protected string dajZrodlo2(string link)
        {
            var request = (HttpWebRequest)WebRequest.Create(link);
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            Stream requestStream = request.GetRequestStream();
            var response = request.GetResponse();
            var stream = response.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            var reader = new StreamReader(stream, Encoding.UTF8);
            string zrodlo = reader.ReadToEnd();
            reader.Close();
            response.Close();
            requestStream.Close();
            stream.Close();
            request.Abort();
            return zrodlo;
        }

        //wyszukanie fragmentu
        protected string wyszukaj(ref string zrodlo, string poczatek, string koniec)
        {
            try
            {
                int wskaznik = zrodlo.IndexOf(poczatek) + poczatek.Length;
                zrodlo = zrodlo.Substring(wskaznik);
                if (zrodlo.Remove(zrodlo.IndexOf(koniec)).Replace("&oacute;", "ó") == ">" || zrodlo.Remove(zrodlo.IndexOf(koniec)).Replace("&oacute;", "ó") == "<")
                    return "";
                return zrodlo.Remove(zrodlo.IndexOf(koniec)).Replace("&oacute;", "ó");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        int liczZdj = 0;

        //pobranie wiekszych plikow np obrazka
        protected string PobierzPlik(string link, string nazwa)
        {
            try
            {
                if (nazwa.Contains(" "))
                    nazwa = nazwa.Remove(nazwa.IndexOf(" "));
                string myStringWebResource = null;
                nazwa = nazwa + ".jpg";
                WebClient myWebClient = new WebClient();
                myStringWebResource = link;
                liczZdj++;
                if (!File.Exists(Application.StartupPath + "\\Obrazy\\" + nazwa))
                {
                    myWebClient.DownloadFile(myStringWebResource, Application.StartupPath + "\\Obrazy\\" + nazwa);
                    myWebClient.CancelAsync();
                    return Application.StartupPath + "\\Obrazy\\" + nazwa;
                }
                else
                {
                    myWebClient.DownloadFile(myStringWebResource, Application.StartupPath + "\\Obrazy\\" + liczZdj + nazwa);
                    myWebClient.CancelAsync();
                    return Application.StartupPath + "\\Obrazy\\" + liczZdj + nazwa;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //wuszukanie kilku fragmentow ze zrodla
        protected string WyszukajWiele(ref string zrodlo, string poczatek, string poczatek2)
        {
            try
            {
                zrodlo = zrodlo.Substring(zrodlo.IndexOf(poczatek) + poczatek.Length);
                string tekst = wyszukaj(ref zrodlo, poczatek2, "<").Trim();
                string cache = " ";
                while (cache != "")
                {
                    tekst += cache.Trim();
                    cache = wyszukaj(ref zrodlo, ">", "<");//.Trim();//.Replace(" ", "");
                }
                return tekst/*.Replace(" ", "")*/.Replace(",", ", ");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //wyszukiwanie fragmentu za pomoca wyrazen regularnych
        protected string regx(string zrodlo, string text)
        {
            Regex regexObj = new Regex(text);
            string result = regexObj.Match(zrodlo).Groups[1].Value;
            return result.Replace("&oacute;", "ó");
        }
    }
}
